//
//  WISProgressView.m
//  WisdomStudy
//
//  Created by  androidlongs on 2017/9/3.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "WISProgressView.h"


@interface WISProgressView ()

//弹窗
@property (nonatomic,retain) UIView *alertView;
//title
@property (nonatomic,retain) UILabel *titleLbl;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//
@property(nonnull,strong) UIActivityIndicatorView *indicatorView ;
@end


@implementation WISProgressView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */


-(instancetype)initWithMessage:(NSString *)message{
    if (self == [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 5.0;
        
        self.alertView.frame = CGRectMake(0, 0, progreW, 100);
        self.alertView.layer.position = self.center;
        
        
        if (message) {
            
            self.msgLbl = [self GetAdaptiveLable:CGRectMake(XLSpace,XLSpace, progreW-2*XLSpace, 20) AndText:message andIsTitle:NO];
            self.msgLbl.textAlignment = NSTextAlignmentCenter;
            
            [self.alertView addSubview:self.msgLbl];
            
//            CGFloat msgW = self.msgLbl.bounds.size.width;
//            CGFloat msgH = self.msgLbl.bounds.size.height;
            
            self.msgLbl.frame = CGRectMake(XLSpace, self.alertView.frame.size.height/2+self.alertView.frame.size.height/10, progreW-2*XLSpace, 20);
        }
        
        self.indicatorView =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        //设置显示位置
         self.indicatorView.center = CGPointMake(progreW/2, self.alertView.frame.size.width/6);
        self.indicatorView.backgroundColor = [UIColor whiteColor];
        
        
        [self.alertView addSubview:self.indicatorView];
        
        
        //计算高度
//        CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
//        self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
        self.alertView.layer.position = self.center;
        
        [self addSubview:self.alertView];
    }
    return self;
    
}
#pragma mark - 弹出 -
- (void)showProgressAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.40, 0.40);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1, 1.0);
    } completion:^(BOOL finished) {
        [self.indicatorView startAnimating];
    }];
}
-(void)closeProgressAlertView{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(0, 0);
    } completion:^(BOOL finished) {
        [self.indicatorView stopAnimating];
        [self removeFromSuperview];
    }];
}
-(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle
{
    UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
    contentLbl.numberOfLines = 0;
    contentLbl.text = contentStr;
    contentLbl.textAlignment = NSTextAlignmentCenter;
    if (isTitle) {
        contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
    }else{
        contentLbl.font = [UIFont systemFontOfSize:14.0];
    }
    
    NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
    mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
    [mParaStyle setLineSpacing:3.0];
    [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStr length])];
    [contentLbl setAttributedText:mAttrStr];
    [contentLbl sizeToFit];
    
    return contentLbl;
}

@end
